/**
 layuiAdmin std 构建
*/

var pkg = require('./package.json');

var gulp = require('gulp');
var uglify = require('gulp-uglify');
var minify = require('gulp-minify-css');
var header = require('gulp-header');
var del = require('del');

//注释
var note = [
  //'/** <%= pkg.name %>-v<%= pkg.version %> <%= pkg.license %> License */\n <%= js %>'
  ''
  ,{pkg: pkg, js: ';'}
]

,destDir = '../webapp' //构建的目标目录

//任务
,task = {
  //压缩 JS
  minjs: function(){
    var src = [
      './src/layuiadmin/**/*.js'
      ,'!./src/layuiadmin/json/**/*.js'
      ,'!./src/layuiadmin/config.js'
      ,'!./src/layuiadmin/lib/extend/**/*.js'
    ];
    
    return gulp.src(src).pipe(uglify({
      output: {
        ascii_only: true //escape Unicode characters in strings and regexps
      }
    }))
    .on('error', function (err) {
        //gutil.log(gutil.colors.red('[Error]'), err.toString());
        console.log(err)
    })
    .pipe(header.apply(null, note))
    .pipe(gulp.dest(destDir + '/layuiadmin'));
  }
  
  //压缩 CSS
  ,mincss: function(){
    var src = [
      './src/layuiadmin/**/*.css'
    ]
     ,noteNew = JSON.parse(JSON.stringify(note));
     
     
    noteNew[1].js = '';
    
    return gulp.src(src).pipe(minify({
      compatibility: 'ie7'
    })).pipe(header.apply(null, noteNew))
    .pipe(gulp.dest(destDir + '/layuiadmin'));
  }
  
  //复制文件夹
  ,mv: function(){    

	//gulp.src('./src/layuiadmin/layui/layui.js')
	//.pipe(gulp.dest(destDir + '/layuiadmin/layui'));
  //  gulp.src('./src/layuiadmin/layui/**/*')
  //  .pipe(gulp.dest(destDir + '/layuiadmin/layui'));
    
    //extend
    gulp.src('./src/layuiadmin/lib/extend/**/*')
    .pipe(gulp.dest(destDir + '/layuiadmin/lib/extend'));
    
    gulp.src('./src/layuiadmin/config.js')
    .pipe(gulp.dest(destDir + '/layuiadmin'));
    
    gulp.src('./src/layuiadmin/tpl/**/*')
    .pipe(gulp.dest(destDir + '/layuiadmin/tpl'));
    
    gulp.src('./src/layuiadmin/style/res/**/*')
    .pipe(gulp.dest(destDir + '/layuiadmin/style/res'));
    
    gulp.src('./src/layuiadmin/style/res/*')
    .pipe(gulp.dest(destDir + '/layuiadmin/style/res'));
    
    return gulp.src('./src/views/**/*')
    .pipe(gulp.dest(destDir + '/views'));
  }
};

//清理 dist
gulp.task('clear', function(done) {
  del.deleteSync(['./dist/*']);
  done();
});

//构建核心源文件（发行版）
gulp.task('default', function(done){
  //清理 dist
  del.deleteSync(['./dist/*']);

  for(var key in task){
    task[key]();
  }
  done();
});